package com.yami.shop.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yami.shop.bean.model.Post;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 用于存储用户发布的动态，包括文字、图片和视频 Mapper 接口
 * </p>
 *
 * @author EXiaoKe
 * @since 2025-04-14
 */
@Mapper
public interface PostsMapper extends BaseMapper<Post> {
    /**
     * 查询点赞数量
     * @param postId
     * @return
     */
    @Select("SELECT COUNT(*) FROM tz_likes WHERE post_id = #{postId}")
    Integer selectLikeCount(@Param("postId") Long postId);

    /**
     * 获取评论数量
     * @param postId
     * @return
     */
    @Select("SELECT COUNT(*) FROM tz_comments WHERE post_id = #{postId}")
    Long selectCommentCount(@Param("postId") Long postId);


    /**
     * 根据话题ID查询关联的帖子列表
     *
     * 查询逻辑：
     * - 通过INNER JOIN关联帖子表(tz_posts)和话题参与表(tz_topic_participations)
     * - 根据指定话题ID筛选出对应的帖子记录
     *
     * @param topicId 话题ID，关联tz_topic_participations表的topic_id字段
     * @return 关联的帖子列表，包含帖子详细信息
     */
    @Select("SELECT p.* FROM tz_posts p " +
            "INNER JOIN tz_topic_participations tp ON p.post_id = tp.post_id " +
            "WHERE tp.topic_id = #{topicId}")
    List<Post> findPostsByTopicId(@Param("topicId") Long topicId);

}

